<template>
	<view class="content">
		<u-search :placeholder="$t('mdAdmin.noName')" :show-action="false" v-model="searchVal"
			@search="gotoSearchMd"></u-search>
		<view class="" v-for="(item,index) in mdList" :key="index">
			<view class="contentPosition"  @click="selectMdToStorage(item)">
				<view class="" style="width: 450rpx;">
					<view class="contentPosition_map">
						<u-icon size="30" color="#262626" name="map"></u-icon>
						<view class="colorStyle fs28 fn" style="margin-left: 10rpx;">
							{{
							  item.address
							}}
						</view>
					</view>
					<view class="color8C8C8C fs24 mltt">
						{{
						  item.province_name +
						  " " +
						  item.city_name +
						  " " +
						  item.district_name +
						  " " +
						  item.address
						}}
					</view>
				</view>
				<view class="colorStyle fs28 fn" style="display: flex; justify-content: end; align-items: center;">
					<view>{{item.distance}}</view>
					<view class="">
						<u-icon color="#262626" name="arrow-right"></u-icon>
					</view>
				</view>
			</view>
			<u-line margin="25rpx 0rpx"></u-line>
		</view>
	</view>
</template>

<script>
	import goodsCart from "@/api/goodscart.js";
	export default {
		data() {
			return {
				longitude: 0,
				latitude: 0,
				searchVal: '',
				page: 1,
				address:'',
				mdList: [],
				status: "nomore",
				loadText: {
					loadmore: "轻轻上拉",
					loading: "努力加载中",
					nomore: "没有更多了~"
				},
					backUrl: "",
			}
		},
		onReachBottom() {
			if (this.page * this.pageSize >= this.row_count) return;
			this.page++;
			let params = {
				loadMore: true,
				page: this.page
			};
			if (this.searchVal) {
				params["keyword"] = this.searchVal;
			}
			if (this.longitude) {
				params["longitude"] = this.longitude;
			}
			if (this.latitude) {
				params["latitude"] = this.latitude;
			}
			this.getMdList(params);
		},
		onLoad(option) {
			this.longitude=option.longitude
			this.latitude=option.latitude
		},
		methods: {
			gotoSearchMd(value) {
				this.page = 1;
				this.getMdList({
					page: this.page,
					keyword: this.searchVal,
					longitude:this.longitude,
					latitude:this.latitude
				});
			},
			getMdList(params) {
				uni.showLoading({
					title: this.$t("home.loading")
				});
				this.$allrequest.shop.getMdList(params).then((res) => {
					uni.hideLoading();
					if (!res.code) {
						if (res.data.list.length) {
							res.data.list.forEach((el) => {
								let [latitude, longitude] = el.coordinate.split(",");
								el.latitude = latitude;
								el.longitude = longitude;
							});
						}
			
						if (params.loadMore) {
							this.mdList = [...this.mdList, ...res.data.list];
						} else {
							this.mdList = res.data.list;
						}
						if (params.readAddress && res.data.list.length) {
							this.address =
								res.data.list[0].province_name +
								res.data.list[0].city_name +
								res.data.list[0].district_name +
								res.data.list[0].address;
						}
						if (!params.readAddress && !res.data.list.length) {
							let mdLocalData = uni.getStorageSync("storeMdData");
							if (mdLocalData) {
								let mdData = JSON.parse(mdLocalData);
								this.address = mdData.address;
							}
						}
						this.row_count = res.data.row_count;
			
						if (this.mdList.length >= parseInt(this.row_count)) {
							this.status = "nomore";
						}
					}
				});
			},
			// 选中门店
			selectMdToStorage(item) {
				try {
					if (!this.isOpen(item)) {
						uni.showToast({
							title: "该店铺已打样！",
							duration: 2000,
							icon: "none"
						});
						return;
					}
			
					uni.setStorageSync("storeMdData", JSON.stringify(item));
					uni.setStorageSync("MdDataReload", "true");
					goodsCart.getCartList();
					let mdBackUrl = uni.getStorageSync("mdBackUrl");
					if (mdBackUrl) {
						uni.reLaunch({
							url: mdBackUrl
						});
						return;
					}
			
					if (!this.backUrl) {
						uni.switchTab({
							url: "/pages/home/home"
						});
					} else {
						let {
							backUrl
						} = this;
			
						if (backUrl === "/alipay-order/orderMeal/orderMeal") {
							backUrl += `?md_id=${item.id}`;
						}
			
						uni.reLaunch({
							url: backUrl
						});
					}
				} catch (error) {
					this.$utils.goBack();
				}
			},
			// 门店是否营业
			isOpen(item) {
				if (item.open_status == 0) return false;
				if (item.shop_time_type == 0) return true;
				if (!item.start_time) return true;
				let date = new Date();
				let hour = date.getHours();
				let minutes = date.getMinutes();
				let [startBefore, startAfter] = item.start_time.split(":");
				let [endBefore, endAfter] = item.end_time.split(":");
				if (hour >= startBefore && hour < endBefore) {
					return true;
				}
				return false;
			},
		}
	}
</script>

<style>
	.content {
		width: 690rpx;
		margin: 0 auto;
	}

	.contentPosition {
		width: 100%;
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-top: 20rpx;
	}

	.contentPosition_map {
		display: flex;
		align-items: center;
	}

	.colorStyle {
		color: #262626;
	}

	.color8C8C8C {
		color: #8C8C8C;
	}

	.fs28 {
		font-size: 28rpx
	}

	.fs24 {
		font-size: 24rpx
	}

	.fn {
		font-weight: 600;
	}

	.mltt {
		margin-left: 40rpx;
		margin-top: 10rpx;
	}
</style>